<template>
  <div class="all">
    <el-table :data="pagination.records" border>
      <el-table-column fixed="left" prop="studentName" label="姓名" width="180"></el-table-column>
      <el-table-column prop="institute" label="学院" width="200"></el-table-column>
      <el-table-column prop="major" label="专业" width="200"></el-table-column>
      <el-table-column prop="grade" label="年级" width="200"></el-table-column>
      <el-table-column prop="clazz" label="班级" width="100"></el-table-column>
      <el-table-column prop="sex" label="性别" width="120"></el-table-column>
      <el-table-column prop="tel" label="联系方式" width="120"></el-table-column>
      <el-table-column fixed="right" label="操作" width="150">
        <template v-slot:default="scope">
          <el-button @click="checkGrade(scope.row.studentId)" type="primary" size="small">编辑</el-button>
          <el-button @click="deleteById(scope.row.studentId)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.current"
      :page-sizes="[6, 10]"
      :page-size="pagination.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total"
      class="page">
    </el-pagination>

    <!-- 编辑对话框-->
    <el-dialog
      title="编辑学生信息"
      v-model="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <section class="update">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="姓名">
            <el-input v-model="form.studentName"></el-input>
          </el-form-item>
          <el-form-item label="学院">
            <el-input v-model="form.institute"></el-input>
          </el-form-item>
          <el-form-item label="专业">
            <el-input v-model="form.major"></el-input>
          </el-form-item>
          <el-form-item label="年级">
            <el-input v-model="form.grade"></el-input>
          </el-form-item>
          <el-form-item label="班级">
            <el-input v-model="form.clazz"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-input v-model="form.sex"></el-input>
          </el-form-item>
          <el-form-item label="电话号码">
            <el-input v-model="form.tel"></el-input>
          </el-form-item>
        </el-form>
      </section>
      <template v-slot:footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, onMounted, getCurrentInstance } from 'vue';

export default {
  name: 'StudentManagement', // 组件名称
  setup() {
    const form = ref({}); // 存储当前学生的编辑信息
    const pagination = ref({
      current: 1, // 当前页
      total: null, // 总记录数
      size: 6, // 每页记录数
      records: [] // 存储学生记录
    });
    const dialogVisible = ref(false); // 控制对话框显示

    // 获取当前 Vue 实例的上下文，访问 $axios、$message 和 $confirm
    const { proxy } = getCurrentInstance();

    // 获取学生信息
    const getStudentInfo = () => {
      proxy.$axios(`/api/students/${pagination.value.current}/${pagination.value.size}`)
        .then((res) => {
          pagination.value = res.data.data;
        })
        .catch((error) => {
          console.log(error);
        });
    };

    // 改变记录条数
    const handleSizeChange = (val) => {
      pagination.value.size = val;
      getStudentInfo();
    };

    // 改变当前页码，重新发送请求
    const handleCurrentChange = (val) => {
      pagination.value.current = val;
      getStudentInfo();
    };

    // 修改学生信息
    const checkGrade = (studentId) => {
      dialogVisible.value = true;
      proxy.$axios(`/api/student/${studentId}`)
        .then((res) => {
          form.value = res.data.data;
        })
        .catch((error) => {
          console.log(error);
        });
    };

    // 删除学生信息
    const deleteById = (studentId) => {
      proxy.$confirm('确定删除当前学生吗？删除后无法恢复', 'Warning', {
        confirmButtonText: '确定删除',
        cancelButtonText: '算了,留着吧',
        type: 'danger',
      })
        .then(() => {
          proxy.$axios({
            url: `/api/student/${studentId}`,
            method: 'delete',
          }).then((res) => {
            console.log(res);
            getStudentInfo();
          });
        })
        .catch(() => {});
    };

    // 提交更新后的信息
    const submit = () => {
      dialogVisible.value = false;
      proxy.$axios({
        url: '/api/student',
        method: 'put',
        data: form.value,
      })
        .then((res) => {
          console.log(res);
          if (res.data.code === 200) {
            proxy.$message({
              message: '更新成功',
              type: 'success',
            });
          }
          getStudentInfo();
        })
        .catch((error) => {
          console.log(error);
        });
    };

    // 关闭编辑对话框
    const handleClose = (done) => {
      proxy.$confirm('确认关闭？')
        .then((_) => {
          done();
        })
        .catch((_) => {});
    };

    // 页面加载时获取学生信息
    onMounted(() => {
      getStudentInfo();
    });

    return {
      pagination,
      dialogVisible,
      form,
      handleSizeChange,
      handleCurrentChange,
      checkGrade,
      deleteById,
      submit,
      handleClose,
    };
  },
};
</script>

<style lang="less" scoped>
.all {
  padding: 0px 40px;
  .page {
    margin-top: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .edit {
    margin-left: 20px;
  }
  .el-table tr {
    background-color: #dd5862 !important;
  }
}
.el-table .warning-row {
  background: #000 !important;
}

.el-table .success-row {
  background: #dd5862;
}
</style>
